package com.stox.ui.javafx.navigator.view;

import java.util.List;
import java.util.Locale;

import javafx.geometry.Side;
import javafx.scene.control.TabPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.Screen;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;

import com.stox.design.Callback;
import com.stox.dto.AbstractModel;
import com.stox.dto.intf.AbstractModelSearchable;
import com.stox.ui.javafx.core.view.View;
import com.stox.ui.javafx.core.widget.AbstractModelSearchPanel;
import com.stox.ui.javafx.core.widget.StoxStage;
import com.stox.ui.javafx.navigator.view.tab.AbstractNavigatorTab;
import com.stox.ui.javafx.navigator.view.tab.BondNavigatorTab;
import com.stox.ui.javafx.navigator.view.tab.CommodityNavigatorTab;
import com.stox.ui.javafx.navigator.view.tab.DerivativeNavigatorTab;
import com.stox.ui.javafx.navigator.view.tab.EquityNavigatorTab;
import com.stox.ui.javafx.navigator.view.tab.ForexNavigatorTab;
import com.stox.ui.javafx.navigator.view.tab.MutualFundNavigatorTab;

@Component
@DependsOn("stoxStage")
public class NavigatorView extends View implements AbstractModelSearchable, InitializingBean{
	private static final long serialVersionUID = 1L;
	
	@Autowired
	private ApplicationContext applicationContext;
	
	@Autowired
	private EquityNavigatorTab equityNavigatorTab;
	
	@Autowired
	private CommodityNavigatorTab commodityNavigatorTab;
	
	@Autowired
	private DerivativeNavigatorTab derivativeNavigatorTab;
	
	@Autowired
	private ForexNavigatorTab forexNavigatorTab;
	
	@Autowired
	private MutualFundNavigatorTab mutualFundNavigatorTab;
	
	@Autowired
	private BondNavigatorTab bondNavigatorTab;
	
	@Autowired
	private AbstractModelSearchPanel abstractModelSearchPanel;
	
	private TabPane tabPane;
	
	public NavigatorView() {
		super();
		tabPane = new TabPane();
		tabPane.setRotateGraphic(true);
		tabPane.setSide(Side.LEFT);
	}
	
	@Override
	public void afterPropertiesSet() throws Exception {
		setTitle(applicationContext.getMessage("Navigator",null, Locale.getDefault()));
		abstractModelSearchPanel.setabstractModelSearchable(this);
		VBox.setVgrow(tabPane, Priority.ALWAYS);
		getContentPane().getChildren().add(new VBox(tabPane, abstractModelSearchPanel));
		
		
	}
	
	public AbstractModelSearchable getAbstractModelSearchable() {
		AbstractNavigatorTab tab = (AbstractNavigatorTab)tabPane.getSelectionModel().getSelectedItem();
		if(null == tab){
			tabPane.getSelectionModel().selectFirst();
			tab = (AbstractNavigatorTab)tabPane.getSelectionModel().getSelectedItem();
		}
		return tab.getabstractModelSearchable();
	}
	
	public AbstractNavigatorTab getSelectedNavigatorTab() {
		return (AbstractNavigatorTab)tabPane.getSelectionModel().getSelectedItem();
	}
	
	@Override
	public void open() {
		if(tabPane.getTabs().isEmpty()){
			tabPane.getTabs().addAll(equityNavigatorTab, mutualFundNavigatorTab, bondNavigatorTab, commodityNavigatorTab, derivativeNavigatorTab, forexNavigatorTab);
			setPrefWidth(Screen.getPrimary().getVisualBounds().getWidth()/5);
			setPrefHeight(StoxStage.getInstance().getContentPane().getBoundsInLocal().getHeight());
			
			setLayoutX(StoxStage.getInstance().getContentPane().getBoundsInParent().getMinX());
			setLayoutY(StoxStage.getInstance().getContentPane().getBoundsInParent().getMinY());
		}
		super.open();
	}

	@Override
	public AbstractModel search(Callback<AbstractModel, Boolean> matcher,
			int start, boolean forward) {
		return getAbstractModelSearchable().search(matcher, start, forward);
	}

	@Override
	public AbstractModel next(boolean forward) {
		return getAbstractModelSearchable().next(forward);
	}

	@Override
	public List<AbstractModel> get() {
		return getAbstractModelSearchable().get();
	}

}
